<template>
  <div class="app-content-wrap" :style="'minHeight:' + gHeight + 'px;'">
    <div class="g-content-title">短信配置
      <span class="notice">(注意：每项最多设置3个电话号码，设置多个接收号码请用‘,’隔开；开启则推送短信通知，关闭不推送)</span>
    </div>
    <div class="type-select">
      <el-form ref="form" label-width="80px" size="small">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="街道">
              <el-select @change="firstLevelChange" v-model="areaIds.areaId1" placeholder="请选择所属街道">
                <el-option label="请选择街道" value=""></el-option>
                <el-option v-for="item in firstArr" :key="item.deptId" :value="item.deptId" :label="item.deptName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="小区" prop="deptId">
              <el-select @change="secondLevelChange" v-model="areaIds.areaId2" placeholder="请选择所属小区">
                <el-option label="请选择小区" :value="areaIds.areaId1"></el-option>
                <el-option v-for="item in secondArr" :key="item.deptId" :value="item.deptId" :label="item.deptName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div v-show="editShow" class="type-wrap">
      <div class="type-content">
        <ul>
          <li class="top_li" v-for="(item, index) in filterTreeData" :key="index">
            <span class="top_font">{{ item.moduleName }}</span>
            <ul>
              <li class="top_font_li" v-for="(item2, index) in item.nodes" :key="index">
                <div class="item">
                  <span class="top_font2">{{ item2.moduleName }}</span>
                  <el-switch active-value="1" inactive-value="0" v-model="item2.smsStatus"></el-switch>
                </div>
                <div class="item">
                  <span class="top_font2">通知电话:</span>
                  <el-input type="text" v-model="item2.phone" placeholder="请输入电话"></el-input>
                </div>
              </li>
            </ul>
          </li>
        </ul>
        <div class="btn-submit">
          <el-button type="primary" @click="handleSave">保 存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
// 导入基础方法
// const Base = base()
// // loading的配置项
// const loadingOpts = {
//   lock: true,
//   text: '拼命加载中...',
//   spinner: 'el-icon-loading',
//   background: 'rgba(0, 0, 0, 0.7)'
// }
export default {
  name: 'typeList',
  data () {
    return {
      gHeight: this.$store.state.gHeight,
      editShow: false, // 编辑区域是否显示
      moduleTree: [],
      firstArr: [],
      secondArr: [],
      codeArr: [], // 场景代码数组
      moduleObj: {
        moduleName: '',
        logo: '',
        parentId: '',
        parentName: '',
        moduleId: '',
        deptId: '',
        moduleCode: '', // 场景代码
        showFlag: '' // 显示隐藏节点
      },
      areaIds: {
        areaId1: '', // 所属1级
        areaId2: '' // 所属2级
      },
      moduleName: ''
    }
  },
  computed: {
    filterTreeData () {
      return this.moduleTree.filter(item => item.nodes.length)
    }
  },
  mounted () {
    this.getList()
    // this.getModuleTree()
    this.getChildList()
  },
  methods: {
    getChildList () {
      // 获取所有可用的二级场景
      let url = this.$base.getUrl('getValidChildModuleList')
      this.$store
        .dispatch('getForm', {
          url: url
        })
        .then((res) => {
          this.codeArr = res
        })
    },
    handleSave () {
      let url = this.$base.getUrl('jiaSysSetting')
      let newArr = []
      this.moduleTree.map(item => {
        if (item.nodes.length) {
          item.nodes.map(item2 => {
            newArr.push({
              moduleId: item2.moduleId,
              phone: item2.phone,
              smsStatus: item2.smsStatus
            })
          })
        }
      })
      let data = JSON.stringify(newArr)
      let params = {
        url: url,
        data: data,
        isJson: true
      }
      this.$store.dispatch('postArrayForm', params).then((res) => {
        if (res.code === 0) {
          this.$message({
            type: 'success',
            message: '保存成功！'
          })
        }
      })
    },
    getModuleTree () {
      const url = this.$base.getUrl('getModuleInfoTree')
      this.$store
        .dispatch('getForm', {
          url: url,
          data: {
            deptId: this.moduleObj.deptId
          }
        })
        .then((res) => {
          if (res.nodes) {
            this.moduleTree = res.nodes
          } else {
            this.$message.error('拉取信息失败，请检查网络连接！')
          }
        })
    },
    // 拉取所属
    getList () {
      const url = this.$base.getUrl('getTree')
      this.$store
        .dispatch('getForm', {
          url: url
        })
        .then((res) => {
          if (res && res.nodes) {
            this.firstArr = res.nodes
          }
        })
    },
    // 一级所属菜单变化
    firstLevelChange (val) {
      this.editShow = false
      this.secondArr = []
      this.areaIds.areaId2 = val
      for (let i of this.firstArr) {
        if (val === i.deptId) {
          this.secondArr = i.nodes || []
        }
      }
      this.moduleObj.deptId = val
    },
    // 二级所属菜单变化
    secondLevelChange (val) {
      this.editShow = !!val
      this.moduleObj = {
        moduleName: '',
        logo: '',
        parentId: '',
        parentName: '',
        moduleId: '',
        deptId: '',
        moduleCode: '', // 场景代码
        showFlag: 1
      }
      this.moduleObj.deptId = val
      this.getModuleTree()
    }
  }
}
</script>
<style lang="less" scoped>
.notice {
  font-weight: normal;
  margin: 20px;
  color: rgb(235, 61, 61);
}
.btn-submit {
  padding-top: 20px;
  text-align: center;
}
.module_set {
  font-weight: bold;
  font-size: 15px;
}

.font_icon {
  width: 600px;
  position: absolute;
  border: 1px solid #e9e9e9;
  z-index: 99999;
  background-color: #e0e0e0;
  border-radius: 5px;
  padding: 10px;
  line-height: 30px;
  left: 0;
  top: 50px;
}

.set_module {
  /*position: relative;*/
  float: left;
  width: 50%;
  padding: 20px;
}

.type-content {
  padding: 20px;
}

.type-content ul > li > ul > li {
  padding-left: 32px;
  margin-bottom: 30px;
  color: #7d7d7d;
  font-weight: 600;
}

.top_li {
  border-bottom: 1px solid #e9e9e9;
  .item {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
  }
}

.top_li button {
  float: right;
  margin-left: 10px;
}

.top_font {
  font-size: 15px;
  color: #333333;
  letter-spacing: 0;
  line-height: 40px;
  font-weight: bold;
  padding-right: 80px;
  padding-left: 10px;
  cursor: pointer;
}

span.top_font:hover {
  font-size: 110%;
  color: #f5a623;
}

.top_font2 {
  display: inline-block;
  width: 100px;
}

.top_font:before {
  content: '';
  line-height: 35px;
  text-align: center;
  background-color: #999999;
  height: 5px;
  width: 5px;
  display: block;
  float: left;
  position: relative;
  bottom: -17.5px;
}

.selectedIcon {
  position: absolute;
  background-color: transparent;
  z-index: 0;
  top: 0;
  padding-left: 20px;
  height: 40px;
}

.font_icon_inner {
  text-align: center;
}

.showFlag {
  float: right;
  padding: 7px 15px;
  font-size: 12px;
  color: rgb(180, 180, 180);
}

.showFlagSub {
  float: right;
  padding: 0 15px;
  line-height: 30px;
  font-size: 12px;
  color: rgb(180, 180, 180);
}
</style>
